/*
 * Copyright 2020 Mike Penz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.iconics.typeface;

import ohos.agp.text.Font;
import ohos.app.Context;
import ohos.utils.Pair;

import com.mikepenz.iconics.typeface.utils.IconicsPreconditions;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mikepenz on 01.11.14.
 */
public class GenericFont implements ITypeface {
    private final String fontRes;
    private List<Pair<String, Character>> chars = new ArrayList<>();
    private final String mappingPrefix;
    private final String fontName;
    private final String author;

    /**
     * 构造方法
     */
    public GenericFont() {
        this("GenericFont", "GenericAuthor", "", "");
    }

    /**
     * 构造方法
     *
     * @param mappingPrefix 匹配规则
     * @param fontRes 字体文件名
     */
    public GenericFont(String mappingPrefix, String fontRes) {
        this("GenericFont", "GenericAuthor", mappingPrefix, fontRes);
    }

    /**
     * 构造方法
     *
     * @param fontName 字体名
     * @param author 作者
     * @param mappingPrefix 匹配规则
     * @param fontRes 字体文件名
     */
    public GenericFont(String fontName, String author, String mappingPrefix, String fontRes) {
        IconicsPreconditions.getInstance().checkMappingPrefix(mappingPrefix);
        this.fontName = fontName;
        this.author = author;
        this.mappingPrefix = mappingPrefix;
        this.fontRes = fontRes;
    }

    /**
     * 注册字体
     *
     * @param name 字体名
     * @param cr 字体
     */
    public void registerIcon(String name, Character cr) {
        getCharacters().add(new Pair<>(getMappingPrefix() + "_" + name, cr));
    }

    @Override
    public String getFontRes() {
        return fontRes;
    }

    @Override
    public Font getRawTypeface(Context context) {
        return FontCreator.createFont(context, getFontRes());
    }

    @Override
    public List<Pair<String, Character>> getCharacters() {
        return chars;
    }

    @Override
    public String getMappingPrefix() {
        return mappingPrefix;
    }

    @Override
    public String getFontName() {
        return fontName;
    }

    @Override
    public String getVersion() {
        return "1.0.0";
    }

    @Override
    public int getIconCount() {
        return getCharacters().size();
    }

    @Override
    public List<String> getIcons() {
        List<Pair<String, Character>> pairs = getCharacters();
        List<String> list = new ArrayList<>();
        for (Pair<String, Character> pair : pairs) {
            list.add(pair.f);
        }
        return list;
    }

    @Override
    public String getAuthor() {
        return author;
    }

    @Override
    public String getUrl() {
        return "";
    }

    @Override
    public String getDescription() {
        return "";
    }

    @Override
    public String getLicense() {
        return "";
    }

    @Override
    public String getLicenseUrl() {
        return "";
    }

    @Override
    public IIcon getIcon(String key) {
        List<Pair<String, Character>> pairs = getCharacters();
        for (Pair<String, Character> pair : pairs) {
            if (pair.f.equals(key)) {
                return new Icon(key, pair.s).withTypeface(this);
            }
        }
        return null;
    }

    /**
     * 图标
     *
     * @since 2021-06-04
     */
    static class Icon implements IIcon {
        private String customName;
        private ITypeface customTypeface;
        private Character character;

        /**
         * 构造方法
         *
         * @param cr 字体
         */
        Icon(Character cr) {
            this.customName = null;
            this.character = cr;
        }

        /**
         * 构造方法
         *
         * @param name 字体名
         * @param cr 字体
         */
        Icon(String name, Character cr) {
            this.customName = name;
            this.character = cr;
        }

        public void setCustomName(String customName) {
            this.customName = customName;
        }

        public void setCustomTypeface(ITypeface customTypeface) {
            this.customTypeface = customTypeface;
        }

        public void setCharacter(Character character) {
            this.character = character;
        }

        @Override
        public String getName() {
            return customName == null ? character.toString() : customName;
        }

        @Override
        public String getFormattedName() {
            return "";
        }

        @Override
        public Character getCharacter() {
            return character;
        }

        @Override
        public ITypeface getTypeface() {
            return customTypeface;
        }

        /**
         * 设置字体
         *
         * @param typeface 字体
         * @return 当前对象
         */
        public Icon withTypeface(ITypeface typeface) {
            this.customTypeface = typeface;
            return this;
        }
    }
}
